<?php
class Useradmin_Data_Addaction {
	
	private $secure;
	private $database;
	
	public function __construct(){
		$this->secure = new Core_Data_Secure;
		$this->database = new Core_Driver_MysqlImproved;
	}
	
	public function addUser(array $postVars){
		
		$postVars = $this->secure->parseData($postVars);
		
		$username = $postVars['username'];
		$realname = $postVars['realname'];
		$password = $postVars['password'];
		$retypePassword = $postVars['retypePassword'];
		$email = $postVars['email'];
		$accessLevel = $postVars['access_level'];
		$banned = $postVars['banned'];
		$this->validatePassword($password, $retypePassword);
		
		$this->database->connect();
		
		$username = $this->database->escape($username);
		$realname = $this->database->escape($realname);
		$password = $this->database->escape($password);
		$email = $this->database->escape($email);
		$accessLevel = $this->database->escape($accessLevel);
		$banned = $this->database->escape($banned);
		
		$this->database->prepare(
			"
				insert into users(users_username, users_realname, users_password, users_email, users_access_level, users_banned) values('$username', '$realname', '$password', '$email', '$accessLevel', '$banned');
				"
		);
		$this->database->query();
		$this->database->dataOperations();
		echo '<meta http-equiv="Refresh" content="0;url='.SERVER_ROOT.'?useradmin">';
	}
	
	private function validatePassword($password, $retypePassword){
		try {
			if($password == $retypePassword){
				return null;
			}
			else {
				throw new Exception("Passwords are incoreect!");
			}
		}
		catch(Exception $e){
			echo $e->getMessage();
		}
		
	}
}